package com.tanhua.server.service.Impl;

import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.autoconfig.template.SmsTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.User;
import com.tanhua.server.service.MqMessageService;
import com.tanhua.server.service.UserFreezeService;
import com.tanhua.server.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lifeihu
 * @date 2021/12/22
 * @TIME:18:10
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @DubboReference
    private UserApi userApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Autowired
    private UserFreezeService userFreezeService;
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private MqMessageService mqMessageService;
    @Override
    public void sendCode(String phone) {
        //校验用户状态--------------------------
        User user = userApi.findByMobile(phone);
        if (user != null) {
            userFreezeService.checkUserStatus(1,user.getId());
        }
        //1. 生成随机验证码
        String code="123456";
//        String code = RandomStringUtils.randomNumeric(6);
        //2. 调用阿里云发送验证码
//        smsTemplate.sendSms(phone, code);
        //3. 将验证码保存到redis
        redisTemplate.opsForValue().set("CHECK_CODE_" + phone, code, Duration.ofMinutes(5));
    }

    @Override
    public Map loginVerification(String mobile, String code) {
        String redisCode = redisTemplate.opsForValue().get("CHECK_CODE_" + mobile);
        if(StringUtils.isEmpty(code)||!code.equals(redisCode)){
            throw new RuntimeException("验证码校验失败");
        }
        redisTemplate.delete("CHECK_CODE_" + mobile);
        //3. 判断用户是否注册
        boolean isNew = false;
        User user = userApi.findByMobile(mobile);
        if(user==null){
            user = new User();
            user.setMobile(mobile);
            user.setPassword(DigestUtils.md5Hex("123456"));
            Long id = userApi.save(user);
            user.setId(id);
            isNew = true;
            //注册环信用户
            String hxUser="hx"+user.getId();
            Boolean flag = huanXinTemplate.createUser(hxUser, "123456");
            if(flag){
                user.setHxUser(hxUser);
                user.setHxPassword(Constants.INIT_PASSWORD);
                userApi.update(user);
            }
        }
        Map<String, Object> tokenMap = new HashMap<String, Object>();
        tokenMap.put("id",user.getId());
        tokenMap.put("mobile",mobile);
        String token = JwtUtils.getToken(tokenMap);
        String type = "0101";//登录
        mqMessageService.sendLogMessage(user.getId(),type,"user",null);
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        result.put("isNew", isNew);
        return result;
    }

}

